package com.lv.LvSpecialRecommendation.service;

import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.dao.AttachmentDao;
import com.aqgj.util.JsonWrapper;
import com.lv.LvSpecialRecommendation.bean.LvSpecialRecommendation;
import com.lv.LvSpecialRecommendation.dao.LvSpecialRecommendationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service("LvSpecialRecommendation")
public class LvSpecialRecommendationManager {

  @Autowired
  private LvSpecialRecommendationDao lvSpecialRecommendationDao;
  @Autowired
  private AttachmentDao attachmentDao;

  public String save(LvSpecialRecommendation ztcSpecialRecommendation){
    return lvSpecialRecommendationDao.save(ztcSpecialRecommendation);
  }

  //搜索线路站点信息（新增特价推荐）
  public HashMap<String,Object> searchLvTourismForSale(String startSite, String endSite) {
    return JsonWrapper.successWrapper(lvSpecialRecommendationDao.searchLvTourismForSale(startSite,endSite));
  }
//根据旅游模板id，旅游特别推荐类型获取特别推荐列表
  public List<LvSpecialRecommendation> getLvSpecialListByModelId(String modelId, Integer type) {
    return lvSpecialRecommendationDao.getLvSpecialListByModelId(modelId, type);
  }
//根据id删除旅游特别推荐
  public void delete(String id){  lvSpecialRecommendationDao.removeById(id); }
//获取旅游特别推荐
  public LvSpecialRecommendation get(String id){  return lvSpecialRecommendationDao.get(id); }
  //获取所有正常推荐
  public HashMap<String, Object> getSpecialALLList(Integer type) {
    List<Map> result = lvSpecialRecommendationDao.getSpecialALLList(type);
    List<Map> mapList = Collections.emptyList();
    List<String> isExitList = new ArrayList<>();
    if (result.size() > 0) {
      mapList = new ArrayList<>();
      one:for (Map map : result) {
        //特价推荐img
        if ("1".equals(map.get("type").toString())) {
          List<Attachment> attachmentList = attachmentDao.findBy("businessId", map.get("id").toString(), "workItem", "specialImg");
          if (attachmentList.size() > 0) {
            map.put("img", getFileBody(attachmentList.get(0).getFileBody()));
          }
        }
        if (isExitList.size() > 0) {
          //当次循环，(特价ID)判断相同线路还是否存在多条班次
          String tourismObj = map.get("id").toString();
          //相同线路的班次
          if (isExitList.contains(tourismObj)) {
            two:for (Map m : mapList) {
              if (m.get("id").toString().equals(map.get("id").toString())){
                BigDecimal price=new BigDecimal(m.get("minPrice").toString());
                m.replace("minPrice",price);
                m.replace("serverDate",map.get("serverDate").toString());
               break two;
              }
            }
            //如果isExitList，这次循环result不添加到mapList
            continue one;
          } else {
            //前面的循环没有则添加
            isExitList.add(tourismObj);
          }
        //第一次循环
        } else {
          String routeObj = map.get("id").toString();
          isExitList.add(routeObj);
        }
        map.put("minPrice", new BigDecimal(map.get("minPrice").toString()));
        mapList.add(map);
      }
    }
    return JsonWrapper.successWrapper(mapList);
  }

  /**
   * 解密base64位
   * @param fileBody 附件base64位图片数据
   * @return 解密后的base64位图片数据
   */
  private String getFileBody(byte[] fileBody) {
    for (int j = 0; j < fileBody.length; ++j) {
      if (fileBody[j] > 0) {
        // 调整异常数据
        fileBody[j] -= 256;
      }
    }
    String bodyInfo = org.apache.commons.codec.binary.Base64.encodeBase64String(fileBody);
    return bodyInfo;
  }

}
